const db=wx.cloud.database()
const ml=db.collection('musicList')
const audioCtx=wx.createInnerAudioContext()
Page({
    data:{
        tab:1,
        item:1,
        playIndex:3,
        state:'paused',
        play:{
            currentTime:0,
            duration:0,
            percent:0,
            title:'',
            singer:'',
            coverImg:''
        },
        list:[
            {'title':'aaa','src':'3'}
        ]
    },
    onLoad(){
        ml.get().then(res=>{
            console.log(res)
            this.setData({
                musicList:res.data,
            })
            this.selectMusic(0)
            this.random()
        })
        audioCtx.onError(()=>{
            console.log('播放失败'+audioCtx.src)
        })
        audioCtx.onEnded(()=>{
            this.next()
        })
        audioCtx.onCanplay(()=>{
            console.log('onCanplay')
            console.log(audioCtx.duration)
            this.setData({
                'play.duration':audioCtx.duration
            })
            if(this.data.state=='running'){
                audioCtx.play()
            }
        })
        audioCtx.onTimeUpdate(()=>{
            this.setData({
                'play.duration':audioCtx.duration,
                'play.currentTime':audioCtx.currentTime,
                'play.percent':audioCtx.currentTime,
            })
        })
    },
    random(){//随机生成推荐歌曲
        let that=this
        let ary = [];
        let musicList=that.data.musicList
        let list=[]
        for(let i=0; i<9; i++){
            ary[i] = i+1;
        }
        ary.sort(function(){
            return 0.5-Math.random();//返回随机正负值
        });
        console.log(ary)
        for(let i=0;i<9;i++){
            list[i]={'title':musicList[ary[i]]['title'],'src':musicList[ary[i]]['coverImg']}
        }
        console.log(list)
        that.setData({list})
    },
    slide(e){
        console.log(e)
        audioCtx.seek(e.detail.value)
    },
    changeItem(e){
        console.log(e.currentTarget.dataset.item)
        this.setData({
            item:e.currentTarget.dataset.item
        })
    },
    changeTab(e){
        console.log(e.detail.current)
        this.setData({
            tab:e.detail.current,
            item:e.detail.current
        })
    },
    selectMusic(index){
        let music=this.data.musicList[index]
        audioCtx.src=music.src
        console.log('-----------')
        console.log(audioCtx.src)
        this.setData({
            playIndex:index,
            play:music,
            'play.currentTime':0,
            'play.duration':0,
            'play.percent':0
        })
    },
    play(){
        audioCtx.play()
        this.setData({
            state:'running'
        })
    },
    pause(){
        audioCtx.pause()
        this.setData({
            state:'paused'
        })
    },
    next(){
        let index=(this.data.playIndex>=this.data.musicList.length-1)?0:this.data.playIndex+1
        this.selectMusic(index)
        if(this.data.state=='running'){
            this.play()
        }
    },
    change(e){
        this.selectMusic(e.currentTarget.dataset.index)
        this.play()
    }
    
})